<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/8/11
 * Time: 上午11:09
 */

namespace Home\Controller;
use Think\Controller;
use Home\Model\ScoreModel;
use Home\Model\UserScoreLogModel;
use Home\Model\ForumAttachmentModel;

class ScoreController extends Controller
{
    public function create()
    {
        $result = array(
            'status' => false
        );
        $hour = (int)date('H',time());
        if($hour == 1)
        {
            UserScoreLogModel::clear();
        }
        $vid = (int)I('get.vid',0);
        if(!empty($_GET['score_score']) && !empty($_GET['score_comment']) && !empty($_GET['hospital_id']) &&
            !empty($_GET['user_id']) &&
            !empty($_GET['user_name']) &&
            !empty($_GET['user_avator']) && UserScoreLogModel::userExists($_GET['user_id']) && UserScoreLogModel::isAllow
            ($_GET['user_id']))
        {
            $scoreID = ScoreModel::insert(I('get.score_score'),I('get.score_comment'),I('get.hospital_id'),I('get.user_id'),I('get.user_name'),I('get.user_avator'),$vid);
            UserScoreLogModel::insertLog(I('get.user_id'));
            $result = array(
                'status' => true
            );
        }
        header('Content-type: application/json');
        $this->ajaxReturn($result,'jsonp');
    }
    public function scores()
    {
        $result = array();
        if(I('get.hosID',0))
        {
            $scores = ScoreModel::getScoreByHospitalID((int)I('get.hosID'),(int)I('get.page',1),(int)I('get.pageSize',10));
            foreach ($scores as $key => $score) {
                $temp = array();
                if($score['vid'])
                {
                    $temp = ForumAttachmentModel::getAttachmentByVid($score['vid']);
                }
                $scores[$key]['image'] = $temp;
            }
            if($scores)
            {
                $result = $scores;
            }
        }
        header('Content-type: application/json');
        $this->ajaxReturn($result,'jsonp');
    }

    public function getVid()
    {
        $uid = (int)I('get.user_id',0);
        $vid = 0;
        if($uid)
        {
            $vid = \Home\Model\UserScoreLogModel::getVidByUserID($uid);
        }
        header('Content-type: application/json');
        $this->ajaxReturn(array('vid' => $vid),'jsonp');
    }
}